﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.Drawing;
using System.Diagnostics;

using Microsoft.WindowsMobile.Status;

namespace iPhoneUI.Widgets
{
    public class MessagingStatusItem : IImageWidget
    {
        private bool m_notify;

        public MessagingStatusItem()
        {
        }

        public string ImageName { get; set; }

        public SystemProperty SystemProperty { get; set; }

        protected override void OnSetOwner()
        {
            RegisterSystemStateChange(SystemProperty);
            using (SystemState state = new SystemState(SystemProperty, true))
                m_notify = ShouldNotify(state.CurrentValue);

            base.OnSetOwner();
        }

        private static bool ShouldNotify(object newValue)
        {
            if (newValue == null)
                return false;
            
            if (newValue.GetType() == typeof(bool))  
                return Convert.ToBoolean(newValue);

            if (newValue.GetType() == typeof(int))
                return Convert.ToInt32(newValue) > 0;

            return false;
        }

        protected override void OnSystemStateChanged(SystemState property, object newValue)
        {
            m_notify = ShouldNotify(newValue);
            base.OnSystemStateChanged(property, newValue);
        }

        protected override void OnDraw(Canvas canvas, Rectangle clipBounds)
        {
            if (m_notify)
                base.OnDraw(canvas, clipBounds);
        }
    }
}
